"""Imports data from my old drupal site
"""
import lxml.etree as ET
from datetime import datetime
import dateutil.parser
import StringIO
import mwlib.scanner
import mwlib.parser
import mwlib.htmlwriter
import re

class DrupalData(object):
    """Represents the data from a drupal site"""
    def __init__(self, source):
        data = open(source)
        self.xmldoc = ET.parse(data).getroot()
        data.close()

    class BlogEntry(object):
        """A stock drupal blog entry, as extracted from XML"""
        def __init__(self, node_id, title, timestamp, body_text, comments = {}):
            self.node_id = node_id
            self.title = title
            self.timestamp = timestamp
            self.comments = comments
            self.body_text = body_text

    class Comment(object):
        def __init__(self, comment_id, parent_id, subject, body_text, timestamp, commenter_name, commenter_email):
            self.comment_id = comment_id
            self.parent_id = parent_id
            self.subject = subject
            self.body_text = body_text
            self.timestamp = timestamp
            self.commenter_name = commenter_name
            self.commenter_email = commenter_email

    def convert_mw_to_html(self, mwtext, preserve_images = False):
        #sanitize known problematic strings by force
        double_url = re.compile(r"\[(http://.*) http://.*\]", re.U)
        clean_mwtext = double_url.sub("\g<1>", mwtext)
        #parse the mediawiki markup
        tokens = mwlib.scanner.tokenize(None, clean_mwtext)
        _parser = mwlib.parser.Parser(tokens)
        article = _parser.parse()
        out = StringIO.StringIO()
        writer = mwlib.htmlwriter.HTMLWriter(out, None)
        writer.write(article)
        markup = out.getvalue()
        #cleanup some of the goofy stuff it adds
        if preserve_images:
            pass
        else:
            images = re.compile(r"<img .* />")
            markup = images.sub("", markup)

        return markup
        
    

    def get_blog_entries(self):
        self.entries = {}
        for entry in self.xmldoc.findall("*/blog"):
            node_id = int(entry.find('nid').text)
            print "node_id: " + str(node_id)
            title = entry.find('title').text
            timestamp = dateutil.parser.parse(entry.find('changed').text)
            revisions  = entry.find('revisions')
            body_text = revisions.xpath('revision[last()]')[0].find('body').text
            body_text = self.convert_mw_to_html(body_text)
            comments = self.getComments(node_id)
            self.entries[node_id] = self.BlogEntry(node_id, title, timestamp, body_text, comments)
            
    def getComments(self, node_id):
        query = "*/comment[nid=%s]" % node_id
        comments = {}
        for comment in self.xmldoc.xpath(query):
            subject = comment.find('subject').text
            body_text = comment.find('body').text
            body_text = self.convert_mw_to_html(body_text)
            comment_id = int(comment.find('cid').text)
            parent_id = int(comment.find('parent').text)
            timestamp = dateutil.parser.parse(comment.find('timestamp').text)
            try:
                commenter_name = comment.find('name').text
                commenter_email = comment.find('mail').text
            except AttributeError:
                commenter_name = None
                commenter_email = None
                
            comments[comment_id] = self.Comment(comment_id, parent_id, subject, body_text, timestamp, commenter_name, commenter_email)
        return comments
            
                
